package com.jt.aop;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.vo.SysResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice   //只对controller层有效
public class SysResultException {
    //当程序发生异常时，如果没有try{}catch{}则会向上抛出
    //该注解只拦截运行时异常
    @ExceptionHandler({RuntimeException.class})
    public Object exception(Exception e,
                               HttpServletRequest request){
        e.printStackTrace();
        String callback = request.getParameter("callback");
        if (StringUtils.hasLength(callback)){
            return new JSONPObject(callback,SysResult.failed());
        }
        return SysResult.failed();
    }
}


















